package indigo.collections
{
	use namespace indigo;
	
	/**
	 * Iterator specifically created for the LinkedList collection 
	 * @author Sebastiaan Holtrop
	 */	
	public class LinkedListIterator implements IIterator
	{
		private var _currentListItem:LinkedListItem;
		private var _linkedList:LinkedList;
		
		/**
		 * Constructor. Usualy called from within the collection. Use myCollection.iterator
		 * @param linkedList The collection to iterate
		 */
		public function LinkedListIterator(linkedList:LinkedList)
		{
			_linkedList = linkedList;
			reset();
		}
		
		
		/**
		 * Resets the Iterator 
		 */		
		public function reset():void
		{
			_currentListItem = null;
		}
		
		
		/**
		 * Returns the next data element in the collection
		 * @return * 
		 */		
		public function next():*
		{
			// Return the data property of the LinkedListItem
			return nextLinkedListItem().data;
		}
		
		
		/**
		 * Returns the next LinkedListItem in the collection
		 * @return * 
		 */		
		internal function nextLinkedListItem():LinkedListItem
		{
			// We're at the start of our list, _currentListItem hasn't been set yet
			if (!_currentListItem)
			{
				// So set it to the first item
				_currentListItem = _linkedList.firstLinkedListItem;
			}
			else
			{
				// Else just get the next one
				_currentListItem = _currentListItem.next;
			}
			return _currentListItem;
		}	
		
		/**
		 * Returns true if there's a next item available in the collection 
		 * @return Boolean
		 */		
		public function hasNext():Boolean
		{
			// We're at the start of our list
			if (!_currentListItem)
			{
				// So check if there's a first list item
				return _linkedList.firstLinkedListItem ? true : false;
			}
			else if (_currentListItem.next)
			{
				// There's a next item available
				return true;
			}
			return false;
		}
	}
}